// Mesh Format Loader JED
// (c) jimon game studio

#ifndef JEH_CMESHFORMATLOADER_JED
#define JEH_CMESHFORMATLOADER_JED

#include "CMeshLoader.h"

#ifdef JE_SUPPORT_FILEFORMAT_JED

#include "IMeshFormatLoader.h"
#include "IJEDManager.h"

namespace je
{
	namespace scene
	{
		//! JED Format Loader
		class CMeshFormatLoader_JED:public je::scene::IMeshFormatLoader
		{
		protected:
			struct SavedSurfaceDataBlocksInfo
			{
				s32 DataBlockId;
				je::video::IHardwareBufferArray::LayerInfo * LayerInfo;
				jeStringw Path;

				u1 Equal(video::IHardwareBufferArray::LayerInfo * OtherLayerInfo)
				{
					if(!memcmp(LayerInfo,OtherLayerInfo,sizeof(video::IHardwareBufferArray::LayerInfo)))
						return true;
					else
						return false;
				}
			};

			struct AttachBuffers
			{
				u8 Id;
				video::IHardwareBuffer * Buffer;
				video::HardwareBufferArrayLayers Layer;
				s32 VariablesPerObject;
				s32 VariablesSize;
				s32 Offset;
				s32 Stride;
				jeStringw Path;

				AttachBuffers()
					:Offset(0),Stride(0)
				{
				}
			};

			typedef jeList<SavedSurfaceDataBlocksInfo*> SavedListType;
			typedef jeList<AttachBuffers*> AttachBuffersListType;

			ISurface * LoadSurface(core::jeJED_SurfaceBlock * Data,AttachBuffersListType & AttachBuffersList,video::IRender * Render);
			jeMesh * LoadMesh(core::jeJED_DataBlock * Data,AttachBuffersListType & AttachBuffersList,video::IRender * Render);

			void SaveSurfaces(jeMesh::SurfacesListType & Surfaces,SavedListType & SavedList,core::jeJED_DataBlock * ParentData);
			void SaveMesh(jeMesh * Mesh,SavedListType & SavedList,core::jeJED_DataBlock * ParentData);

		public:
			//! Constructor
			CMeshFormatLoader_JED();

			//! Destructor
			~CMeshFormatLoader_JED();

			//! Load Mesh
			jeMesh * LoadMesh(core::IBuffer * DataBuffer,video::IRender * Render);

			//! Save Mesh
			void SaveMesh(core::IBuffer * DataBuffer,jeMesh * Mesh);

			//! Return true if mesh loader can work with this data
			u1 CanWorkWithThis(core::IBuffer * DataBuffer);

			//! Return Mesh Format
			MeshFormat GetMeshFormat()
			{
				return MF_JED;
			}
		};
	}
}

#endif
#endif
